package org.edu.modules.noderes.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.course.service.IVeCuzCourseService;
import org.edu.modules.noderes.entity.VeCuzNodeRes;
import org.edu.modules.noderes.service.IVeCuzNodeResService;
import org.edu.modules.resource.mapper.VeResResourceMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags={"课程内容"})
@RestController
@RequestMapping({"/course/veCuzNodeRes"})
public class VeCuzNodeResController
        extends BaseController<VeCuzNodeRes, IVeCuzNodeResService>
{
    private static final Logger log = LoggerFactory.getLogger(VeCuzNodeResController.class);
    @Autowired
    IVeCuzCourseService veCuzCourseService;
    @Autowired
    VeResResourceMapper veResResourceMapper;
    @Autowired
    private IVeCuzNodeResService veCuzNodeResService;
    @Value("${edu.path.urlPre}")
    private String fileUrlPre;

    @AutoLog("课程内容-分页查询")
    @ApiOperation(value="分页查询", notes="课程内容-分页查询")
    @PostMapping({"/list"})
    public Result<?> queryPageList(VeCuzNodeRes veCuzNodeRes, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<VeCuzNodeRes> queryWrapper = QueryGenerator.initQueryWrapper(veCuzNodeRes, req.getParameterMap());
        Page<VeCuzNodeRes> page = new Page(pageNo.intValue(), pageSize.intValue());
        IPage<VeCuzNodeRes> pageList = this.veCuzNodeResService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog("课程内容-通过id查询")
    @ApiOperation(value="通过id查询", notes="课程内容-通过id查询")
    @PostMapping({"/queryById"})
    public Result<?> queryById(@RequestParam(name="id", required=true) String id)
    {
        VeCuzNodeRes veCuzNodeRes = (VeCuzNodeRes)this.veCuzNodeResService.getById(id);
        if (veCuzNodeRes == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(veCuzNodeRes);
    }

    @AutoLog("课程内容-新增信息")
    @ApiOperation(value="新增信息", notes="课程内容-新增信息")
    @PostMapping({"/add"})
    public Result<?> add(@RequestBody VeCuzNodeRes veCuzNodeRes)
    {
        this.veCuzNodeResService.save(veCuzNodeRes);
        return Result.OK("添加成功！");
    }

    @AutoLog("课程内容-修改信息")
    @ApiOperation(value="修改信息", notes="课程内容-修改信息")
    @PostMapping({"/edit"})
    public Result<?> edit(@RequestBody VeCuzNodeRes veCuzNodeRes)
    {
        this.veCuzNodeResService.updateById(veCuzNodeRes);
        return Result.OK("编辑成功!");
    }

    @AutoLog("课程内容-通过id删除")
    @ApiOperation(value="通过id删除", notes="课程内容-通过id删除")
    @PostMapping({"/delete"})
    public Result<?> delete(@RequestParam(name="id", required=true) String id)
    {
        this.veCuzNodeResService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog("课程内容-批量删除")
    @ApiOperation(value="批量删除", notes="课程内容-批量删除")
    @PostMapping({"/deleteBatch"})
    public Result<?> deleteBatch(@RequestParam(name="ids", required=true) String ids)
    {
        this.veCuzNodeResService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @AutoLog("课程内容-通过章节内容查询")
    @ApiOperation(value="章节内容查询", notes="课程内容-章节内容查询")
    @PostMapping({"/chapterList"})
    public Result<?> chapterList(VeCuzNodeRes veCuzNodeRes, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<VeCuzNodeRes> queryWrapper = QueryGenerator.initQueryWrapper(veCuzNodeRes, req.getParameterMap());
        Page<Map<String, Object>> page = new Page(pageNo.intValue(), pageSize.intValue());
        queryWrapper.setEntity(veCuzNodeRes);
        IPage<Map<String, Object>> pageList = this.veCuzNodeResService.pageMaps(page, queryWrapper);
        for (Map item : pageList.getRecords()) {
            if (item.get("sourcefile") != null) {
                item.replace("sourcefile", this.fileUrlPre + item.get("sourcefile"));
            }
        }
        return Result.OK(pageList);
    }

    @AutoLog("课程内容-通过章节查询资源")
    @ApiOperation(value="通过章节查询资源", notes="课程内容-通过章节查询资源")
    @PostMapping({"/chapterRes"})
    public Result<?> chapterRes(@RequestParam(name="nodeid", required=false) Integer nodeid)
    {
        return Result.OK(this.veCuzNodeResService.selectNodeRes(nodeid));
    }
}
